package com.zb.config.inteceptor;

import cn.dev33.satoken.router.SaRouter;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjUserSet;
import com.zb.redis.RedisUserLoginTokenUtil;
import com.zb.service.ZjUserSetService;
import com.zb.utils.IgnorePathUtil;
import com.zb.utils.RequestUtil;
import com.zb.utils.TokenMobileUtil;
import com.zb.utils.TokenUserUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 工单客户报单移动端拦截器
 * @Author WEN
 * @Date 2023/7/28 0028
 **/
@Configuration
public class UserInteceptor implements HandlerInterceptor {
    private ZjUserSetService userSetService;

    public UserInteceptor(ZjUserSetService userSetService) {
        this.userSetService = userSetService;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (RequestUtil.isPc()) {
            return true;
        }
        // 是否客户移动端的请求路径 || 判断是否需要登录，如果无需登录，则进入判断
        if (RequestUtil.isUserMobileLogin()) {
            return true;
        }
        if (!RequestUtil.isUserMobileHasLogin()) {
            AccounLoginInfo loginInfo = TokenMobileUtil.getAccountLoginInfo(request);
            if (null == loginInfo) {
                Long custAccId = TokenUserUtil.getCustAccIdByMobildUserCustCode(request);
                ZjUserSet userSet = userSetService.getById(custAccId);
                if (null != userSet && userSet.getMobileHasLogin()) {
                    throw new LoginException("您使用的平台开启了需要登录后才能操作，您当前登录已过期，请重新登录！");
                }
                return true;
            }
        }
        // 判断是否公共有路径
        if (RequestUtil.isPublicMobile()) {
            AccounLoginInfo loginInfo = TokenMobileUtil.getAccountLoginInfo(request);
            if (null == loginInfo) {
                throw new LoginException("登录已过期，请重新登录！");
            }
            return true;
        }
        if (RequestUtil.isMobileNetwork()) {
            return true;
        }
        if (SaRouter.match(IgnorePathUtil.ignorePath()).isHit) {
            return true;
        }
        // 判断是否移动端上传文件
        if (TokenMobileUtil.isMobileUploadFile()) {
            return true;
        }
        String accessToken = TokenUserUtil.getToken(request);
        if (StringUtils.isBlank(accessToken) || !RedisUserLoginTokenUtil.hasByAccessToken(accessToken)) {
            throw new LoginException("登录已过期，请重新登录！");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
